package com.example.shopping.controller;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从 session 中获取用户信息（假设登录后将用户存入 session）
        Object user = request.getSession().getAttribute("loginUser");

        if (user == null) {
            // 用户未登录，重定向到登录页
            // LoginInterceptor.java
            response.sendRedirect("/shopping/user/login");
            return false; // 不继续执行 Controller 方法
        }

        return true; // 放行请求
    }
}